//
//  API.swift
//  ShortVideo
//
//  Created by SYQM on 2022/8/3.
//

import Moya

enum API {
    case testOne(parameters: [String:Any]) // 有参
    case testTwo // 无惨
    case testThree(email: String, password: String) // 多参数
    case testFour(paramrters: [String:Any], imageData:Data)  //资源文件
    
    // 登录
    case login(parameters: [String:Any])  //有参
    // 注册
    case register(parameters: [String:Any])   // 无参数
    // 发送密码
    case sendCode(parameters: [String:Any])
    // 忘记密码
    case forgetPassword(parameters: [String:Any])
    
    // 修改用户信息
    case eidtUserInfo(parameters: [String:Any])
    
    
    case uploadResource(imageData:Data)  //资源文件
    
    // 意见反馈
    case feedback(parameters: [String:Any])
}



extension API: TargetType{
    var baseURL: URL {
        return URL.init(string: BASE_URL)!
    }
    
    var path: String {
        switch self{
        case .testOne:
            return "test/one"
        case .testTwo:
            return "test/two"
        case .testThree(let email,_):
            return "test/three" + email
        case .testFour:
            return "test/four"
            
        case .login:
            return "user/login"
        case .register:
            return "user/register"
        case .sendCode:
            return "email/code"
        case .uploadResource:
            return "upload/resource"
        case .forgetPassword:
            return "user/forgetpass"
        case .eidtUserInfo:
            return "user/editInfo"
        case .feedback:
            return "feedback"
        }
    }
    
    var method: Moya.Method {
        switch self {
        case .login:
            return .post
        case .register:
            return .post
        case .uploadResource:
            return .post
        case .forgetPassword:
            return .post
        case .eidtUserInfo:
            return .post
        case .feedback:
            return .post
        default:
            return .get
        }
    }
    
    var task: Task {
        switch self{
        case .testOne(parameters: let parameters):
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)
            
        case .testTwo:
            return .requestPlain
            
        case .testThree(email: let email, password: let password):
            return .requestParameters(parameters: ["email": email, "password": password], encoding: URLEncoding.default)
            
        case .testFour(paramrters: let paramrters, imageData: let imageData):
            let formData = MultipartFormData(provider: .data(imageData), name: "file", fileName: "zhou.png", mimeType: "image/png")
                        return .uploadCompositeMultipart([formData], urlParameters: paramrters)
            
        case let .login(parameters):
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)
        case .register(parameters: let parameters):
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)
        
        case .sendCode(parameters: let parameters):
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)
        case .uploadResource(imageData: let imageData):
            let formData = MultipartFormData(provider: .data(imageData), name: "image[0]", fileName: "image.png", mimeType: "image/png")
            return .uploadCompositeMultipart([formData], urlParameters: [:])
        case .forgetPassword(parameters: let parameters):
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)
        case .eidtUserInfo(parameters: let parameters):
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)
            
        case .feedback(parameters: let parameters):
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)
        }
    }
    
    var headers: [String : String]? {
        switch self {
        default:
            return ["Content-Type":"application/x-www-form-urlencoded"]
        }
    }
    
    
}
